package com.cheer.controller;

import com.cheer.common.Result;
import com.cheer.param.PersonSaveParam;
import com.cheer.service.AuthService;
import com.cheer.service.VoteService;
import com.cheer.vo.ExVoteIndexVO;
import com.cheer.vo.ExVotePersonVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author cheer
 */
@RestController
@RequestMapping("/vote")
public class VoteController {

    @Autowired
    private VoteService voteService;

    @Autowired
    private AuthService authService;

    @GetMapping("/index")
    public Result<List<ExVoteIndexVO>> person(HttpSession session) {
        return Result.success(voteService.index(authService.getOpenid(session)));
    }

    @GetMapping("/search")
    public Result<List<ExVotePersonVO>> search(@RequestParam String keyword, HttpSession session) {
        return Result.success(voteService.search(StringUtils.trimToNull(keyword), authService.getOpenid(session)));
    }

    @PostMapping("/on")
    public Result<Integer> postVote(@RequestParam Long personId, HttpSession session) {
        return Result.success(voteService.postVote(personId, authService.getOpenid(session)));
    }

    @PostMapping("/off")
    public Result cancelVote(@RequestParam Long personId, HttpSession session) {
        voteService.cancelVote(personId, authService.getOpenid(session));
        return Result.success();
    }

    @GetMapping("/detail")
    public Result<ExVotePersonVO> detail(@RequestParam Long personId, HttpSession session) {
        return Result.success(voteService.detail(personId, authService.getOpenid(session)));
    }


    /**
     * 爬虫数据导入接口
     * @return
     */
    @PostMapping("/save")
    public Result save(@RequestBody PersonSaveParam personSaveParam) {
        voteService.save(personSaveParam);
        return Result.success();
    }


}
